package com.dq.domain.goods.stock;

import com.dq.domain.sys.user.user.BossUser;
import com.dq.vo.baseVo.VO;
import com.dq.domain.base.BaseEntity;

import com.dq.domain.goods.Goods;
import com.dq.domain.goods.Product;
import com.dq.domain.sys.section.SysSection;
import com.dq.utils.TimeUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;

/**
 * @author  cf
 */
@Data
@Entity
@ApiModel("库存变动记录")
public class StockChangeRecord extends BaseEntity implements VO.ToVO {
    @ApiModelProperty(value = "门店",hidden = true)
    @ManyToOne
    protected SysSection org;

    @ApiModelProperty(value = "商品",hidden = true)
    @ManyToOne
    protected Goods goods;
    @ApiModelProperty(value = "商品, 区别不同规格不同库存",hidden = true)
    @ManyToOne
    protected Product product;

    @ApiModelProperty("商品变动类型")
    @Enumerated(EnumType.ORDINAL)
    protected ChangeType changeType;
    @ApiModelProperty("商品变动方向")
    @Enumerated(EnumType.ORDINAL)
    @NotNull(message = "变动类型不能为空")
    protected ChangeDirection changeDirection;
    @ApiModelProperty("库存变动数量: 出入库数量")
    @NotNull(message = "库存变动数量不能为空")
    protected Long num;
    @ApiModelProperty("扣减/添加后剩余库存")
    protected Long thenStock;

    @ApiModelProperty("备注")
    protected String remarks;

    @ApiModelProperty(value = "审批状态",hidden = true)
    protected StockStatus stockStatus;


    @ApiModelProperty("出库价格")
    protected BigDecimal reducePrice;
    @ApiModelProperty("出售员工")
    @ManyToOne
    protected BossUser sellUser;

    @ApiModelProperty("前台传参: 商品ID")
    @Transient
    public Long goodsId;
    @ApiModelProperty("前台传参: 门店ID, 不传默认是登录账号门店下")
    @Transient
    public Long orgId;
    @ApiModelProperty("前台传参: 出售员工")
    @Transient
    protected Long sellUserId;

    public StockChangeRecord() {
    }
    public StockChangeRecord(ChangeType changeType, ChangeDirection changeDirection, Long num) {
        this.changeType = changeType;
        this.changeDirection = changeDirection;
        this.num = num;
    }

    public enum ChangeType{
        /**  */
        loss("消耗"),
        sell("售出"),
        /** 用于入库类型, 避免ChangeType为空 */
        adjust("调整"),
        ;
        String name;
        ChangeType(String name){
            this.name = name;
        }
    }
    public enum ChangeDirection{
        /**  */
        add("入库"),
        reduce("出库"),
        ;
        String name;
        ChangeDirection(String name){
            this.name = name;
        }
    }

    public enum StockStatus{
        /**  */
        waitApproval("待审批"),
        ApprovalSuccess("审批通过"),
        ApprovalFail("审批不通过");
        String name;
        StockStatus(String name){
            this.name = name;
        }
    }
    @Data
    @ApiModel(value = "库存变动记录视图")
    public static class Vo implements VO {
        protected Long id;
        @ApiModelProperty("门店")
        protected Long orgId;
        protected String orgName;
        @ApiModelProperty("商品信息")
        protected Long goodsId;
        protected String goodsNo;
        protected String goodsName;
        @ApiModelProperty("商品建议价")
        protected BigDecimal goodsPrice;
        @ApiModelProperty("商品主图")
        protected String goodsSmallImgUrl;
        @ApiModelProperty("product信息")
        protected String productSku;
        @ApiModelProperty("商品分类")
        public String categoryName;
        public Long categoryId;

        @ApiModelProperty("库存变动类型")
        protected String changeTypeEn;
        protected String changeTypeCn;
        @ApiModelProperty("库存变动方向")
        protected String changeDirectionEn;
        protected String changeDirectionCn;
        @ApiModelProperty("库存变动数量")
        protected Long num;
        @ApiModelProperty("历史库存")
        protected Long thenStock;
        @ApiModelProperty(value = "备注")
        private String remarks;
        @ApiModelProperty("审批状态")
        protected String stockStatusCn;
        protected String stockStatusEn;

        @ApiModelProperty(value = "创建时间")
        private String createTime;
        @ApiModelProperty("创建人员")
        protected Long createUserId;
        protected String createUserUserRealcName;
        @ApiModelProperty(value = "修改时间")
        private String updateTime;
        @ApiModelProperty("修改人员")
        protected Long updateUserId;
        protected String updateUserUserRealcName;
        @ApiModelProperty(value = "出库价格")
        private BigDecimal reducePrice;
        @ApiModelProperty("出售员工")
        protected Long sellUserId;
        protected String sellUserRealcName;
    }
    @Override
    public VO toVO(boolean isRecursion) {
        Vo vo = new Vo();
        vo.id =this.getId();
        vo.orgId = this.getOrg() == null ? null : this.getOrg().getId();
        vo.orgName = this.getOrg() == null ? "" : this.getOrg().getSimpleName();
        vo.goodsId = this.getGoods() == null ? null : this.getGoods().getId();
        vo.goodsName = this.getGoods() == null ? "" : this.getGoods().getGoodsName();
        vo.goodsNo = this.getGoods() == null ? "" : this.getGoods().getGoodsNo();
        vo.goodsPrice = this.getGoods() == null ? null : this.getGoods().getPrice();
        vo.goodsSmallImgUrl = this.getGoods() == null ? "" : this.getGoods().getSmallImgUrl();
        vo.productSku = this.getProduct() == null ? "" : this.getProduct().getSku();
        vo.categoryId = this.getGoods() == null ? null : (this.getGoods().getCategory() == null ? null : this.getGoods().getCategory().getId());
        vo.categoryName = this.getGoods() == null ? null : (this.getGoods().getCategory() == null ? null : this.getGoods().getCategory().getTitle());

        vo.changeTypeEn = fmt(this.getChangeType().toString());
        vo.changeTypeCn = fmt(this.getChangeType().name);
        vo.changeDirectionEn = fmt(this.getChangeDirection().toString());
        vo.changeDirectionCn = fmt(this.getChangeDirection().name);

        vo.num = fmt(this.getNum());
        vo.thenStock = fmt(this.getThenStock());
        vo.remarks = fmt(this.getRemarks());
        vo.stockStatusCn = fmt(this.getStockStatus().name);
        vo.stockStatusEn = fmt(this.getStockStatus().toString());

        vo.createTime = TimeUtil.longToString(this.createTime, TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.createUserId = this.getCreateUser() == null ? null : this.getCreateUser().getId();
        vo.createUserUserRealcName = this.getCreateUser() == null ? "" : this.getCreateUser().getUserRealcName();
        vo.updateTime = TimeUtil.longToString(this.lastModifyTime, TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.updateUserId = this.getUpdateUser() == null ? null : this.getUpdateUser().getId();
        vo.updateUserUserRealcName = this.getUpdateUser() == null ? "" : this.getUpdateUser().getUserRealcName();

        vo.reducePrice = fmt(this.getReducePrice());
        vo.sellUserId = this.getSellUser() == null ? null : this.getSellUser().getId();
        vo.sellUserRealcName = this.getSellUser() == null ? "" : this.getSellUser().getUserRealcName();

        return vo;
    }

}
